/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package maxent.mr.writable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.apache.hadoop.io.Writable;
import stanford.tagger.FeatureKey;

/**
 *
 * @author hadoop
 */
public class FeatureKeysWritable implements Writable {

    Set<FeatureKeyWritable> listFeatureKey = new HashSet<FeatureKeyWritable>();

    public FeatureKeysWritable() {
    }

    public void addFeature(FeatureKeyWritable fkWritable){
        listFeatureKey.add(fkWritable);
    }

    public Set<FeatureKey> getFeatureKeys() {
        Set<FeatureKey> fkSet = new HashSet<FeatureKey>();

        Iterator<FeatureKeyWritable> fkwIt = listFeatureKey.iterator();

        while(fkwIt.hasNext()){
            FeatureKeyWritable fkw = fkwIt.next();
            FeatureKey fk = new FeatureKey(fkw.getNum().get(),fkw.getVal().toString(),fkw.getTag().toString());
            fkSet.add(fk);
        }

        return fkSet;
    }
    

    public void write(DataOutput out) throws IOException {
        Iterator<FeatureKeyWritable> fkIt = listFeatureKey.iterator();

        while(fkIt.hasNext()){
            fkIt.next().write(out);
        }
    }

    public void readFields(DataInput in) throws IOException {
        Iterator<FeatureKeyWritable> fkIt = listFeatureKey.iterator();

        while(fkIt.hasNext()){
            fkIt.next().readFields(in);
        }
    }

    

}
